package com.yyge.controller.admin.dashboard;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyge.common.util.WaimaiThreadLocal;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.dashboard.resp.ReportResp;
import com.yyge.controller.admin.dashboard.strategy.ContamerReportStrategy;
import com.yyge.controller.admin.login.resp.AdminLoginResp;
import com.yyge.entity.Contamer;
import com.yyge.entity.Merchandiser;
import com.yyge.entity.Product;
import com.yyge.service.ContamerService;
import com.yyge.service.MerchandiserService;
import com.yyge.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "管理后台-数据报表")
@RestController
@RequestMapping(value = "/api/dashboard")
public class DashBoardController {
    /**
     * 会员
     */
    @Autowired
    private ContamerService contamerService;

    /**
     * 商家
     */
    @Autowired
    private MerchandiserService merchandiserService;

    /**
     * 菜品
     */
    @Autowired
    private ProductService productService;

    /**
     * spring自动同一个接口中类存在map中
     */
    @Autowired
    private Map<String, ContamerReportStrategy>  contamerReportStrategyMap;


    @ApiOperation(value = "数据")
    @GetMapping(value = "/data")
    public R<Map<String,Object>> data(){
        AdminLoginResp adminLoginResp= WaimaiThreadLocal.getCurrent();
        // 会员总数量
        long allNum = this.contamerService.count();

        //本月新注册会员
        Date begin= DateUtil.beginOfMonth(new Date());
        Date end=DateUtil.endOfDay(DateUtil.endOfMonth(new Date()));
        LambdaQueryWrapper<Contamer> contamerWrapper=new LambdaQueryWrapper<>();
        contamerWrapper.between(Contamer::getCreateTime,begin,end);
        long currentMonthNum=this.contamerService.count(contamerWrapper);


        //菜品总数量
        long countProductNum=0;

        //成交总金额(订单表)
        long totalMoney=0;
        long totalOrders=0;
        //待审核的商家数
        long notApprovedNum=0;

        //未成交订单 （取消订单）
        long outstandingOrders=0;

        //管理员
        if(adminLoginResp.getType()==1){
            //菜品总数量
            countProductNum= this.productService.count();

           //成交总金额(订单表)

            //待审核的商家数
            LambdaQueryWrapper<Merchandiser> merchandiserLambdaQueryWrapper=new LambdaQueryWrapper<>();
            merchandiserLambdaQueryWrapper.eq(Merchandiser::getStatus,"0");
            notApprovedNum=this.merchandiserService.count(merchandiserLambdaQueryWrapper);
        }else{
            //某个商家 菜品总数量
            LambdaQueryWrapper<Product> productLambdaQueryWrapper=new LambdaQueryWrapper<>();
            productLambdaQueryWrapper.eq(Product::getMerchantId, adminLoginResp.getId());
            countProductNum= this.productService.count(productLambdaQueryWrapper);

            //成交总金额(订单表)
        }

        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("allNum",allNum);
        dataMap.put("currentMonthNum",currentMonthNum);
        dataMap.put("countProductNum",countProductNum);
        dataMap.put("totalMoney",totalMoney);
        dataMap.put("totalOrders",totalOrders);
        dataMap.put("notApprovedNum",notApprovedNum);
        dataMap.put("outstandingOrders",outstandingOrders);
        System.out.println("有东西么");
        System.out.println(dataMap);
        return R.okHasData(dataMap);
    }

    @ApiOperation(value = "会员查询报表,(1本周，2本月，3今年）")
    @GetMapping(value = "/contamerReport/{index}")
    public R<ReportResp> contamerReport(@PathVariable(value = "index") int index){
        System.out.println(this.contamerReportStrategyMap);

        String name= ContamerReportStrategy.beanNames[index-1];
        //取得某种策略算法
        ContamerReportStrategy contamerReportStrategy = this.contamerReportStrategyMap.get(name);
        //xAxis  标识[]
        //series 数据[]
        ReportResp stringObjectMap = contamerReportStrategy.calcReport();

        return R.okHasData(stringObjectMap);
    }
}
